[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Menu.MenuContainer.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Menu.MenuContainer.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Menu
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.ScriptReferences;

    /// <summary>
    /// A webcontrol that can be shown inside Menu
    /// </summary>    
    [ToolboxData("<{0}:MenuComponentBase ></{0}:MenuComponentBase")]
    [ParseChildren(false)]
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Resources.js.jquery.jquery.js")]
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Controls.Menu.MenuContainer.js")]
    public class MenuContainer : WebControl
    {
        private string content;
        private string title;

        /// <summary>
        /// Initializes a new instance of the <see cref="MenuContainer"/> class. 
        /// </summary>
        public MenuContainer()
        {
            ContentCssClass = "menu_buttons";
            Initialize();
        }

        /// <summary>
        /// Gets or sets Content.
        /// </summary>
        /// <value>
        /// The content.
        /// </value>
        [Bindable(true)]
        [Category("Appearance")]
        public string Content
        {
            get
            {
                return content;
            }

            set
            {
                content = value;
            }
        }

        /// <summary>
        /// Gets or sets ContentCssClass.
        /// </summary>
        /// <value>
        /// The content css class.
        /// </value>
        [Bindable(true), Category("Appearance")]
        public string ContentCssClass
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets Title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        [Bindable(true)]
        [Category("Appearance")]
        public string Title
        {
            get
            {
                return title;
            }

            set
            {
                title = value;
            }
        }

        /// <summary>
        /// Gets TagKey.
        /// </summary>
        /// <value>
        /// The tag key - <see cref="HtmlTextWriterTag.Div" />.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// Adds id and css class attributes
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute("class", ContentCssClass);
            writer.AddAttribute("id", "__" + ID);
        }

        /// <summary>
        /// Includes associated embedded resources.
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ScriptIncluder.IncludeResources(this);
        }

        /// <summary>
        /// Renders the control
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void Render(HtmlTextWriter writer)
        {
            RenderLabel(writer);
            base.Render(writer);
        }

        /// <summary>
        /// Renders the contents
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(Content);
            base.RenderContents(writer);
        }

        /// <summary>
        /// Renders the label associated with the container
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected virtual void RenderLabel(HtmlTextWriter writer)
        {
            writer.AddAttribute("class", "menu_tag");
            writer.AddAttribute("id", "__" + ID + "Label");
            writer.AddAttribute("onclick", "BM.MenuContainer.trigger(this, '__" + ID + "');");
            writer.RenderBeginTag("div");
            writer.Write(Title);
            writer.RenderEndTag();
        }

        private void Initialize()
        {
            CssClass = "menuBase";
        }
    }
}